/*******************************************************************************
 * Copyright (c) 2010 - 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Lars Vogel <lars.Vogel@gmail.com> - Bug 419770
 *******************************************************************************/
package cn.elwy.rcp.kit.parts;

import javax.annotation.PostConstruct;

import org.eclipse.e4.ui.model.application.ui.MUIElement;
import org.eclipse.e4.ui.model.application.ui.basic.MBasicFactory;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.services.EMenuService;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;

public class ExplorerPart {
	// Ouch !! self-injection !!
	// @Inject
	private EPartService ps;
	// @Inject
	private IPresentationEngine renderer;
	private MWindow hostWin;

	@PostConstruct
	public void createControls(Composite parent, EMenuService menuService) {
		parent.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Label lblNewLabel = new Label(parent, SWT.NONE);
		lblNewLabel.setText("New Label");
		// more code...
		TableViewer viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.MULTI);
		Table table = viewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		// more code

		// register context menu on the table
		menuService.registerContextMenu(viewer.getControl(), "cn.elwy.rcp.kit.popupmenu.0");
	}
	// @PostConstruct
	// public void createComposite(Composite parent, EMenuService menuService) {
	// IWorkbenchWindow wbw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	// hostWin = (MWindow) wbw.getService(MWindow.class);
	// ContextInjectionFactory.inject(this, hostWin.getContext());
	// menuService.registerContextMenu(parent, "org.eclipse.ui.help.aboutAction");
	// Composite comp = new Composite(parent, SWT.NONE);
	// comp.setLayout(new FillLayout());
	// // setControl(comp);
	//
	// // Create the model
	// MUIElement hostModel = createModel();
	//
	// // Render the model...by 4.2 M4 we expect that this will be
	// // available as a method in the EModelService
	// renderModel(comp, hostWin, hostModel);
	// }

	@SuppressWarnings("restriction")
	private MUIElement createModel() {
		// Create the view's model
		// MPart viewModel = ps.createPart(viewId);
		// viewModel.setCloseable(false);

		// Create a second 'simple' e4-based part
		MPart simple = MBasicFactory.INSTANCE.createPart();
		simple.setLabel("Simple Part");
		simple.setContributionURI("platform:/plugin/HostedView/hostedview.AReallySimplePart");
		// simple.setIconURI(viewModel.getIconURI());

		// Wrap the parts in a stack
		MPartStack stack = MBasicFactory.INSTANCE.createPartStack();
		// stack.getChildren().add(viewModel);
		stack.getChildren().add(simple);

		return stack;
	}

	@SuppressWarnings("restriction")
	private void renderModel(Composite parent, final MWindow hostWin, final MUIElement hostModel) {
		// This is subtle; unless the element is hooked into the model it won't fire events
		hostWin.getSharedElements().add(hostModel);

		// Render it
		renderer.createGui(hostModel, parent, hostWin.getContext());

		// Clean up the shared elements list once we're done
		parent.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				hostWin.getSharedElements().remove(hostModel);
			}
		});
	}
}